/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.service.impl;

import java.util.List;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.mapper.SupplierMapper;
import com.ktjiaoyu.utils.Page;
import com.ktjiaoyu.service.SupplierService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author 科泰集团教研团队
 * @packageName com.ktjiaoyu
 * @fileName Supplier.java
 * @createTime 2024/10/18 23:25:38
 * @lastModify 2024/10/18 23:25:38
 * @version 1.0.0
 */
@Service
public class SupplierServiceImpl implements SupplierService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierServiceImpl.class);

    @Autowired
    private SupplierMapper supplierMapper;

    public Page<Supplier> findPage(Supplier entity, Integer pageNo, Integer pageSize)  throws Exception{
        Page<Supplier> page = null;
        try {
           page = new Page<Supplier>();
           page.setPageSize(pageSize);
           // 查询总数，计算总页数
           Long count = supplierMapper.count(entity);
           page.setTotalCount(count.intValue());
           page.setPageNo(pageNo);
           // 查询分页数据
           List<Supplier> data = supplierMapper.getPage(entity, page.offset(), page.getPageSize());
           page.setData(data);
       } catch (Exception e) {
            LOGGER.error("执行SupplierServiceImpl.findPage(Supplier, Integer, Integer)方法出现异常.", e);
            throw e;
        }
        return page;
    }


    @Override
    public boolean saveOrUpdate(Supplier entity) throws Exception {
        try {
            if(entity.getId() == null){
                // 无主键则新增
                return supplierMapper.insert(entity) > 0;
            }else{
                // 有主键则修改
                return supplierMapper.update(entity) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行SupplierServiceImpl.saveOrUpdate(Supplier, Integer, Integer)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public Supplier findById(Long id) throws Exception{
        try {
            return supplierMapper.getById(id);
        } catch (Exception e) {
            LOGGER.error("执行SupplierServiceImpl" +
                    ".findById(Long)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public Supplier findBySupCode(String supCode) throws Exception{
        try {
            return supplierMapper.getBySupCode(supCode);
        } catch (Exception e) {
            LOGGER.error("执行SupplierServiceImpl" +
                    ".findBySupCode(String)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public List<Supplier> findAll() throws Exception{
        try {
            return supplierMapper.getAll();
        } catch (Exception e) {
            LOGGER.error("执行SupplierServiceImpl" +
                    ".findAll()方法出现异常.", e);
            throw e;
        }
    }
}
